#include "dui.h"

listqueue * linkqueuecreat()//创建链式队列
{
    queue * head = (queue*)malloc(sizeof(queue));
    if (head == NULL)
    {
        printf("%s error\n",__func__);
        return NULL;
    }
    
    head->data = (datatype) 0;
    head->next = NULL ;
    listqueue *q = (listqueue*)malloc(sizeof(listqueue));
    if(q == NULL)
    {
        printf("%s error\n",__func__);
        return NULL;
    }
    q->front = head;
    q->rear = head;
    return q;
}

int linkqueueenter(listqueue*q,datatype data )//入队
{
    queue* temp = (queue*)malloc(sizeof(queue));
    if(temp == NULL)
    {
        printf("%s reeor\n",__func__);
        return -1;
    }
    temp-> data = data;

    temp->next = q->rear->next;
    q->rear->next = temp;
    q->rear = temp;
    return 0;
}
int linkqueueempty(listqueue *q)//判空
{
    return q->front == q->rear ? 0:-1;
}

datatype linkqueuedelete(listqueue *q)//出队
{
    if(linkqueueempty(q) == 0)
    {
        printf ("linkempty\n");
        return (datatype) -1;
    }
    queue *temp = q->front->next;
    datatype olddata = temp->data;

    q->front->next = temp->next;

    return olddata;
}

void linkqueueshow(listqueue*q)//遍历
{
    if(linkqueueempty(q)==0)
    {
        printf("linkempty\n");
        return ;
    }
    queue *h = q->front;
    while(h->next != NULL)
    {
        printf("-%d",h->next->data);
        h=h->next;
    }
    putchar(10);
}
